package com.controller;

import com.biz.IBookBiz;
import com.entity.BookImg;
import com.entity.Books;
import java.io.File;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.AjaxResult;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/books")
public class BookAction {

    @Autowired
    private IBookBiz biz;


    public IBookBiz getBiz() {
        return biz;
    }

    public void setBiz(IBookBiz biz) {
        this.biz = biz;
    }

    //列表
    @RequestMapping("/list")
    @ResponseBody
    public PageInfo getAll(Books books, HttpSession session,@RequestParam(defaultValue = "1") Integer pageNum){
        PageInfo pageInfo=biz.getAll(books,pageNum);
       return pageInfo;
    }

    @RequestMapping("/getbyid/{n}")
    public String getById(int bid, @PathVariable(value = "n") int n, HttpSession session){
        Books books=biz.getById(bid);
        session.setAttribute("books",books);
        if(n==0){
            return "back/book_detail";
        }
        else if(n==1){
            return "back/book_mod";
        }else{
            return "/details";
        }
    }

    //根据bookid 获取图书详情
    @RequestMapping("/getByBookId")
    public String getByBookId(int bookid,HttpSession session){
        Books books=biz.getByBookId(bookid);
        session.setAttribute("books",books);
        return "checkOrder";
    }



    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest req, HttpServletResponse res) throws Exception
    {
        try
        {
//           // 上传文件路径
            String filePath=req.getServletContext().getRealPath("imgs");
//           // 上传并返回新文件名称
            String fileName=file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath+"/"+fileName));
            BookImg bookImg=new BookImg();
           int bid=biz.getNewBook()+1;
            bookImg.setBookid(bid);
            bookImg.setImgpath("imgs"+"/"+fileName);
            biz.addBookImg(bookImg);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequestMapping("/add")
    public String add(Books books){
        biz.addBook(books);
        return "back/book_list";

    }

    @RequestMapping("/mod")
    public String  mod(Books books){
        biz.modBook(books);
        return "back/book_list";
    }

    @RequestMapping("/del")
    public String del(int bid){
        biz.delBook(bid);
        return "back/book_list";
    }
}
